package com.powernobug.servlet;

import com.powernobug.util.WdConstant;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Field;

/**
 * @description:
 * @project: project1-no-bug
 * @package: com.powernobug.servlet
 * @author: HuangWeiLong
 * @date: 2024/8/27 21:50
 */
public class ServiceInitServlet extends CommonServlet{
    public void init(){
        ApplicationContext applicationContext = (ApplicationContext) getServletContext().getAttribute(WdConstant.APPLICATION_CONTEXT_NAME);
        Field[] declaredFields = this.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            Class<?> type = declaredField.getType();
            if(type.getName().contains("Service")){
                Object serviceBean = applicationContext.getBean(type);
                try {
                    declaredField.set(this,serviceBean);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
